﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLServerHealthCheck.SQLServer;
using System.Configuration;
using SQLServerHealthCheck.Model;

namespace SQLServerHealthCheck.PL
{
    public partial class OverviewIssues : Form
    {
        HealthCheckDataContext dc;
        public OverviewIssues(HealthCheckControlPanel controlPanel)
        {

            InitializeComponent();
        }

        private void DatabaseCheckResults_Load(object sender, EventArgs e)
        {
            try
            {
                dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);

                var qry = from d in dc.Issues
                          orderby
                            d.IssueType.Severity descending,
                            d.IssueType.Category.Description,
                            d.Description
                          select new
                          {
                              Server = GetServerName(d.TargetId, d.IssueType.ScopeId),
                              Instance = GetInstanceName(d.TargetId, d.IssueType.ScopeId),
                              Database = GetDatabaseName(d.TargetId, d.IssueType.ScopeId),
                              Description = d.Description,
                              Category = d.IssueType.Category.Description,
                              Scope = d.IssueType.Scope.Description,
                              Severity = d.IssueType.Severity
                          };

                //select d;
                dataGridView1.DataSource = qry.ToList();



            }

            catch (Exception ex)
            {


                MessageBox.Show(ex.Message, "HealthCheck", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private object GetDatabaseName(int? targetId, int scopeId)
        {
            if (targetId.HasValue)
            {
                String databaseName = "";
                //Server
                if (scopeId == 1)
                {
                }
                //Service
                else if (scopeId == 2)
                {
                }
                //Database
                else if (scopeId == 3)
                {
                    Database database = dc.Databases.FirstOrDefault(d => d.DatabaseID == targetId.Value);
                    if (database != null)
                    {
                        databaseName = database.DatabaseName;
                    }
                }
                //Table
                else if (scopeId == 4)
                {
                    Table table = dc.Tables.FirstOrDefault(t => t.TableID == targetId.Value);
                    if (table != null)
                    {
                        databaseName = table.Database.DatabaseName;
                    }
                }

                return databaseName;
            }
            else
            {
                return "";
            }
        }

        private object GetInstanceName(int? targetId, int scopeId)
        {
            if (targetId.HasValue)
            {
                String instanceName = "";
                //Server
                if (scopeId == 1)
                {
                }
                //Service
                else if (scopeId == 2)
                {
                    Service service = dc.Services.FirstOrDefault(s => s.ServiceID == targetId.Value);
                    if (service != null)
                    {
                        instanceName = service.Name;
                    }
                }
                //Database
                else if (scopeId == 3)
                {
                    Database database = dc.Databases.FirstOrDefault(d => d.DatabaseID == targetId.Value);
                    if (database != null)
                    {
                        instanceName = database.Service.Name;
                    }

                }
                //Table
                else if (scopeId == 4)
                {
                    Table table = dc.Tables.FirstOrDefault(t => t.TableID == targetId.Value);
                    if (table != null)
                    {
                        instanceName = table.Database.Service.Name;
                    }

                }

                return instanceName;
            }
            else
            {
                return "";
            }
        }

        private String GetServerName(int? targetId, int scopeId)
        {
            if (targetId.HasValue)
            {
                String serverName = "";
                //Server
                if (scopeId == 1)
                {
                    Server server = dc.Servers.FirstOrDefault(s => s.ServerID == targetId.Value);
                    if (server != null)
                    {
                        serverName = server.Name;
                    }
                }
                //Service
                else if (scopeId == 2)
                {
                    Service service = dc.Services.FirstOrDefault(s => s.ServiceID == targetId.Value);
                    if (service != null)
                    {
                        serverName = service.Server.Name;
                    }
                }
                //Database
                else if (scopeId == 3)
                {
                    Database database = dc.Databases.FirstOrDefault(d => d.DatabaseID== targetId.Value);
                    if (database != null)
                    {
                        serverName = database.Service.Server.Name;
                    }

                }
                //Table
                else if (scopeId == 4)
                {
                    Table table = dc.Tables.FirstOrDefault(t => t.TableID== targetId.Value);
                    if (table != null)
                    {
                        serverName = table.Database.Service.Server.Name;
                    }

                }

                return serverName;
            }
            else
            {
                return "";
            }
        }

        private void rvDatabaseandFiles_Load(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}
